﻿using BussinessEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;
using Db4objects.Db4o.Linq;
using Db4objects.Db4o;
using Configurations;

namespace DataAccessLayer
{
    public class ReceiptDetailDA
    {
        public ActionResult<ReceiptDetail> Insert(ReceiptDetail entity, IObjectContainer conn)
        {
            ActionResult<ReceiptDetail> rs = new ActionResult<ReceiptDetail>();
            try
            {
                var temp = (from ReceiptDetail p in conn
                            where p.ServiceID == entity.ServiceID && p.ReceiptID == entity.ReceiptID
                            select p).FirstOrDefault<ReceiptDetail>();
                if (temp == null)
                {
                    Receipt hd = (from Receipt p in conn
                                 where p.ReceiptID == entity.ReceiptID
                                 select p).FirstOrDefault<Receipt>();
                    if (hd != null)
                    {
                        Service dv = (from Service p in conn
                                     where p.ServiceID == entity.ServiceID
                                     select p).FirstOrDefault<Service>();
                        if (dv != null)
                        {
                            //dv.IsReally = hd.IsReally = false;
                            entity.Service = dv;
                            entity.Receipt = hd;
                            conn.Store(entity);
                            if (dv.ServiceCategory == ServiceState.ThayPhanCung)
                                dv.Quantity -= entity.Quantity;
                            conn.Store(dv);
                            rs.Object = entity;
                        }
                        else
                            rs.AddException(new Exception("Không tồn tại dịch vụ"));
                    }
                    else
                        rs.AddException(new Exception("Không tồn tại hóa đơn"));
                }
                else
                    rs.AddException(new Exception("Trùng khóa chính"));
            }
            catch (Exception ex)
            {
                rs.AddException(ex);
            }
            return rs;
        }

        public ActionResult<ReceiptDetail> Insert(List<ReceiptDetail> ListCT,IObjectContainer connect)
        {
            ActionResult<ReceiptDetail> rs = new ActionResult<ReceiptDetail>();
            foreach (var i in ListCT)
            {
                ActionResult<ReceiptDetail> rsa = Insert(i, connect);
                if (!rsa.Success)
                    rs.Exceptions.AddRange(rsa.Exceptions);
            }
            return rs;
        }

        public ActionResult<ReceiptDetail> Delete(ReceiptDetail entity, IObjectContainer conn)
        {
            ActionResult<ReceiptDetail> rs = new ActionResult<ReceiptDetail>();
                try
                {
                    
                    conn.Delete(entity);
                    var temp = (from Service p in conn
                                where p.ServiceID == entity.ServiceID
                                select p).FirstOrDefault<Service>();
                    if (temp.ServiceCategory == ServiceState.ThayPhanCung)
                        temp.Quantity += entity.Quantity;
                    conn.Store(temp);
                }
                catch (Exception exx)
                {
                    rs.AddException(exx);
                }
                return rs;
        }

        public ActionResult<ReceiptDetail> Delete(string MaHD, IObjectContainer conn)
        {
            ActionResult<ReceiptDetail> rs = new ActionResult<ReceiptDetail>();
            try
            {
                var temp = (from Receipt p in conn
                            where p.ReceiptID == MaHD
                            select p).FirstOrDefault<Receipt>();
                if (temp != null)
                {
                    List<ReceiptDetail> ListCT = (from ReceiptDetail p in conn
                                             where p.ReceiptID == MaHD
                                             select p).ToList<ReceiptDetail>();
                    foreach (var i in ListCT)
                    {
                        ActionResult<ReceiptDetail> rsa = Delete(i, conn);
                        if (!rsa.Success)
                            rs.Exceptions.AddRange(rsa.Exceptions);
                    }
                    return rs;
                }
                else
                    rs.AddException(new Exception("Mã hóa đơn : " + MaHD + " không tồn tại."));
            }
            catch (Exception exx)
            {
                rs.AddException(exx);
            }
            return rs;
        }

        public ActionResult<ReceiptDetail> GetAll(string MaHD)
        {
            ActionResult<ReceiptDetail> rs = new ActionResult<ReceiptDetail>();
            using (var conn = DbConnector.OpenDB())
            {

                try
                {
                    rs.Array = (from ReceiptDetail p in conn
                                where p.ReceiptID == MaHD
                                select p).ToList<ReceiptDetail>();
                    foreach (var i in rs.Array)
                    {
                        i.Service = (from Service p in conn
                                    where p.ServiceID == i.ServiceID
                                    select p).FirstOrDefault<Service>();
                        i.Receipt = (from Receipt p in conn
                                    where p.ReceiptID == i.ReceiptID
                                    select p).FirstOrDefault<Receipt>();
                    }

                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }

        public ActionResult<ReceiptDetail> GetSingle(string MaHD, string MaDV)
        {
            ActionResult<ReceiptDetail> rs = new ActionResult<ReceiptDetail>();
            using (var conn = DbConnector.OpenDB())
            {
                try
                {
                    var temp = (from ReceiptDetail p in conn
                                where p.ServiceID == MaDV && p.ReceiptID == MaHD
                                select p).FirstOrDefault<ReceiptDetail>();
                    if (temp != null)
                    {
                        rs.Object = temp;
                        rs.Object.Service = (from Service p in conn
                                            where p.ServiceID == rs.Object.ServiceID
                                            select p).FirstOrDefault<Service>();
                        rs.Object.Receipt = (from Receipt p in conn
                                            where p.ReceiptID == rs.Object.ReceiptID
                                            select p).FirstOrDefault<Receipt>();
                    }
                    else
                        rs.AddException(new Exception("Mã dịch vụ : " + MaDV + " không tồn tại."));
                }
                catch (Exception ex)
                {
                    rs.AddException(ex);
                }
                finally
                {
                    conn.Close();
                }
                return rs;
            }
        }
    }
}
